import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import { OrderId } from '@/globals/order';
import { Trigger } from '@/trigger';
import BuffCommon from 'code/Core/Common/BuffCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 潜行
 **/
export default class DZ_Sneak extends AbilityBase {
    static instance: DZ_Sneak;
    buffType: AppBuffType = {
        id: 'buff_DZ_Sneak',
        art: this.art,
        name: this.abilityName,
        ubertip: '隐形，并在下次攻击时增加伤害',
        dur: 6,
    };
    constructor(id: string = 'Aqx1') {
        super(id);
        DZ_Sneak.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onBeforApplyAttack(this.onAttackEffect.bind(this));

        let t = new Trigger();
        t.registerAnyUnitEvent(EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER);
        t.addAction(this.orderAction.bind(this));
    }
    orderAction() {
        let order = GetIssuedOrderId();
        if (order != OrderId.Attack && order != OrderId.Smart && order != OrderId.Attackonce) return;
        let target = GetOrderTargetUnit();
        if (target == null) return;
        let u = GetTriggerUnit();
        let dis = MathUtil.distanceBetweenUnits(target, u);
        if (dis > 600) return;
        if (!BuffUtil.hasUnitBuff(u, this.buffType.id)) return;
        DestroyEffect(AddSpecialEffect('Abilities\\Spells\\Orc\\FeralSpirit\\feralspiritdone.mdl', GetUnitX(u), GetUnitY(u)));
        let { x, y } = MathUtil.polarProjection(GetUnitX(target), GetUnitY(target), 125, GetUnitFacing(target) + 180);
        SetUnitX(u, x);
        SetUnitY(u, y);
        EXSetUnitFacing(u, GetUnitFacing(target));
        DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Orc\\FeralSpirit\\feralspiritdone.mdl', u, 'origin'));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        // BuffUtil.addBuff(source, this.buffType.id, source);
        BuffCommon.addBuff({ target: source, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
        UnitCommon.普攻冷却(source);
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onAttackEffect(d: DamageEventData) {
        let buff = BuffUtil.getUnitBuff(d.source, this.buffType.id);
        if (buff == null) return;
        let lv = this.getAbilityLevel(d.source);
        let dmg = (UnitCommon.取总力量(d.source) + UnitCommon.取总敏捷(d.source)) * 0.3 * lv;
        d.initDamage += dmg;
        buff.destroy();
    }
}
